package artibella.core
{

	import artibella.core.events.NavEvent;
	import artibella.core.data.Config;
	import artibella.utils.StringsUtils;
	import asual.swfaddress.SWFAddress;
	import flash.errors.IllegalOperationError;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	
	

	/**
	 * ...
	 * @author pbordachar
	 */

	public class Nav extends EventDispatcher
	{
		private static var _instance:Nav;
		
		private var _data_OK:Boolean;
		
		private var _defaultTitle:String;
		private var _swfaddress:String;
		private var _indicesPage:String; // "1_1_0" ou "1_1_0/1" ( si part de rub )
		
		private var _titleSeparator:String;
		
		private var _first:Boolean;
		private var _jsSendTag:String;

		public function Nav( c:PrivateClass )
		{
			if( !c ) {
				throw new IllegalOperationError( 'Error : singleton pattern' );
			}
		}
		
		public static function get instance():Nav
		{
			return _instance || ( _instance = new Nav( new PrivateClass() ) );
		}
		
		///////  S I T E M A P
		
		public function start():void
		{
			_data_OK = true;
			_first = true;
			
			_defaultTitle = Config.getString( "seo.defaultTitle" );
			_titleSeparator = Config.getString( "seo.separator" );
			_jsSendTag = Config.getString( "misc.tags.@js" );
			
 			addEventListener( NavEvent.CALL, _onCall );

			_getCurrentAddress();
		}

		///////  C H A N G E
		
		private function _getCurrentAddress():void
		{
			//trace( "Nav -- _getCurrentAddress()" )

			_swfaddress = SWFAddress.getValue(); 
			
			if( _swfaddress != "" && _swfaddress != "/" ){
				_swfaddress = _clean( _swfaddress );
				_indicesPage = Config.getIndices( _swfaddress );
			} 

			if ( _indicesPage == null ) {
				
				_indicesPage = "0";
				
				//var landing:String = Config.getString( "sitemap.@default" );
				//_indicesPage = ( landing != "" ) ? landing : "0";
				//_swfaddress = Config.getAdresse( _indicesPage );
				//_swfaddress = _clean( _swfaddress );
			}
			
			loadContent( _indicesPage );
		}
		
		public function checkAddress( adresse:String="", dest:String="0" ):void
		{
			//trace( "Nav -- checkAddress()" )

			if( _data_OK ){
				
				var adresseBrowser:String = _clean( adresse );
				
				if ( adresseBrowser != _swfaddress ){
					
					//_indicesPage = dest; // argh!
					//var landing:String = Config.getString( "sitemap.@default" );
					//if ( dest == "" ) dest = ( landing != "" ) ? landing : "0";
					_indicesPage = dest;
					
					//var indices:String;
					if ( adresseBrowser != "" && adresseBrowser != "/" ) {
						var indices:String = Config.getIndices( adresseBrowser );
						indices = Config.getIndices( adresseBrowser );
						if ( indices != null ){
							_indicesPage = indices;
						}
  					}  
					
					//if ( indices == null || _indicesPage == indices || _indicesPage == dest ) return;

					loadContent( _indicesPage );
				} 
			} 
		}
		
		private function _onCall( e:NavEvent ):void
		{
			trace( e );
			
			if ( e.go != "" ) {
				
				var indices:String = e.go;	
				
				if ( indices != _indicesPage ) {
					
					_indicesPage = indices;
					loadContent( _indicesPage );
				}
				
			} else {
				
				throw new Error( "* * * Nav Error : missing property * * *" );
			}
		}
		
		// !!!
		
		public function get indicesPage():String
		{
			return _indicesPage;
		}
		
		// - - -
		
		public function loadContent( str:String ):void
		{
			//trace( "Nav//loadContent( " + str + " )" );
			
			_setSWFAddress( str );

			dispatchEvent( new NavEvent( NavEvent.CHANGE, str ) );
			
			if ( !_first ) {
				sendTag( _swfaddress );
			}
			
			_first = false;
		}
		
		///////  U T I L S
		
		private function _clean( adresse:String ):String
		{
			if ( adresse.charAt( 0 ) == "/" ){
				adresse = adresse.substring( 1, adresse.length );
			}
			
			if ( adresse.charAt( adresse.length - 1 ) == "/" ){
				adresse = adresse.substring( 0, adresse.length-1 );
			}

			return adresse;
		}
		
		/////// B R O W S E R

		public function getSWFAddress():String
		{
			return SWFAddress.getValue();	
		}
		 
		private function _setSWFAddress( s:String ):void
		{
			//trace( "Nav -- _setSWFAddress()" );

			_swfaddress = Config.getMarquage( s );
			SWFAddress.setValue( _swfaddress );	
		}
		
		/// - - -
		
		public function showLink( lien:String="" ):void
		{
			if( lien != "" ){
				SWFAddress.setStatus( lien );
			} else {
				SWFAddress.resetStatus();
			}
		}
		
		public function setPageTitle( s:String ):void
		{
			var titre:String = "";
			
			if( _defaultTitle != ""){
				titre += _defaultTitle;
			} 
			
			if( StringsUtils.htmlOut( s ) != "" ) {
				titre += _titleSeparator + StringsUtils.htmlOut( s );
			}
			
			SWFAddress.setTitle( titre );
		}
		
		// - - -
		
		public function sendTag( str:String ):void
		{
			if ( ExternalInterface.available ) {
				if ( str != "" && str != "/" ){
					ExternalInterface.call( _jsSendTag, str );
				}
			}
		}
	}
	
	/////////////////////////////////////
	///////  S W F  A D D R E S S ///////
	/////////////////////////////////////

	SWFAddress.onChange = function(){
		
		Nav.instance.checkAddress( SWFAddress.getValue() );
	}

	//////////////  F I N  //////////////
}

/// - - -

class PrivateClass { }